package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	[SWF(width=500, height=500)]
	public class PixelateTest extends Sprite
	{
		[Embed(source="Picture.jpg")]
		public var myFace:Class;
		
		public var bmd:BitmapData;
		public var bmp:Bitmap;
		
		public var SIZE:int = 10;
		
		public function PixelateTest()
		{
			var tick:int = getTimer();
			bmd = pros(new myFace().bitmapData);
			trace(getTimer() - tick);
			bmp = new Bitmap(bmd);
			addChild(bmp);
		}
		
		public function pros(data:BitmapData):BitmapData
		{
			var h:int = data.height;
			var w:int = data.width;
			
			var r:uint = 0;
			var g:uint = 0;
			var b:uint = 0;
			
			var vect:Vector.<uint> = data.getVector(data.rect);
			
			var i:int=0;
			var j:int=0;
			var u:int=0;
			var v:int=0;
			var px:uint=0;
			var color:uint;
			
			data.lock();
			for( u=0 ; u<=data.width-SIZE ; u+=i )
			{
				for( v=0 ; v<=data.height-SIZE ; v+=j )
				{
					for( i=0 ; i<SIZE ; ++i )
					{
						for( j=0 ; j<SIZE ; ++j )
						{
							px = vect[((i+u)*w)+j+v];
							r += (px >> 16) & 0xFF;
							g += (px >> 8) & 0xFF;
							b += px & 0xFF;
						}
					}
					r = r/(SIZE*SIZE);
					g = g/(SIZE*SIZE);
					b = b/(SIZE*SIZE);
					
					if(r > 0xFF) r = 0xFF;
					if(g > 0xFF) g = 0xFF;
					if(b > 0xFF) b = 0xFF;
					
					color = (r << 16)+(g << 8)+b;
					
					data.fillRect(new Rectangle(v,u,SIZE,SIZE),color);
				}
			}
			data.unlock();
			
			return data;
		}
	}
}